package com.example.rabbitmq4.listener;

import com.example.rabbitmq4.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class AckListener {
    @RabbitListener(queues = Constants.ACK_QUEUE)
    public void queueListener(Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            System.out.println("接收到消息:" + message + channel);
            int ret = 3/0;
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            try {
                //第二个参数表示是否批量确认，第三个参数表示当拒绝消息的时候是否将该消息重新进入队列
                channel.basicNack(deliveryTag,false,true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
